package com.loongtech.bi.action.biflow;

import com.loongtech.bi.action.Base2BiSearchAction;
import com.loongtech.bi.manager.flow.HadoopQueryManager;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.core.util.Time;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.Map.Entry;

/**
 * 
 * ClassName: ItemLeftnumRankingFlowNewAction
 * 
 * @Description: 物品排行流水
 * @author liuchen
 * @date 2017-5-18 下午2:09:49
 */
@Controller
@Scope("prototype")
@RequestMapping("/flow/itemLeftnumRankingFlow")
public class ItemLeftnumRankingFlowNewAction extends Base2BiSearchAction {
	private static final long serialVersionUID = 1L;
	@Autowired
	HadoopQueryManager hadoopQueryManager;

	/**
	 * 
	 * @Description: TODO 获取参数
	 * @param @param request
	 * @param @param response
	 * @param @param callback
	 * @return void
	 * @throws
	 * @author liuchen
	 */
	@RequestMapping(value = "initParam.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
		try {
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("res", "0");
			result.put("serverItmes", serverItmes);
			printToJson(response, result, callback);
		} catch (Exception e) {
			e.printStackTrace();
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
			return;
		}
	}

	protected Date day = new Date(Time.getBeginningOfDay(Time.currentTime() - 1 * Time.DAY));

	@RequestMapping(value = "search.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String params,
			String callback) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<String, Object> m = jsonToMap(params);
			String itemid = (String) m.get("itemid");
			String serverid = (String) m.get("serverid");

			// itemid 不能为空
			if (StringUtils.isBlank(itemid)) {
				result.put("res", "1");
				result.put("msg", "物品id不能为空!");
				printToJson(response, result, callback);
				return;
			}
			if (HadoopQueryManager.VISIT_QUEUE.offer("ItemLeftnumRankingFlowNewAction")) {
				System.out.println("VISIT_QUEUE offer ItemLeftnumRankingFlowNewAction ok!");

				StringBuffer sql = new StringBuffer();
				sql.append("select serverid,userid,itemid,sum(leftnum) as sumleftnum,0 from totalroleitemleftnum where dt = '"
						+ Time.getDateStr(day) + "' ");

				if (!"0".equals(serverid)) {
					sql.append(" and serverid = ");
					sql.append(serverid);
				}

				if (!StringUtils.isBlank(itemid)) {
					sql.append(" and itemid = '");
					sql.append(itemid.trim());
					sql.append("' ");
				}

				sql.append(" group by serverid,userid,itemid order by sumleftnum desc limit 10000 ");

				Entry<List<String>, List<Object[]>> ret = hadoopQueryManager.executeSQL(sql.toString());
				if (null == ret || null == ret.getKey() || ret.getKey().isEmpty() || null == ret.getValue()
						|| ret.getValue().isEmpty()) {
                    result.put("res", new ArrayList<Object[]>());
                    result.put("data", "");
                    printToJson(response, result, callback);
					return;
				}
				List<Object[]> lo = ret.getValue();
				for (Object[] o : lo) {
					o[0] = BiPlatformManager.getServerNameById((String) o[0]);
				}

				result.put("res", "0");
				result.put("data", lo);
				printToJson(response, result, callback);
			} else {
				System.out.println("VISIT_QUEUE offer ItemLeftnumRankingFlowNewAction failed!");
				result.put("res", "1");
				result.put("msg", "当前系统正被访问，为保证服务器稳定，请稍后再试!");
				printToJson(response, result, callback);
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
		}
	}

	@Override
	public void search() {
		// TODO Auto-generated method stub

	}

	@Override
	protected String csvContent() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String csvFileName() {
		// TODO Auto-generated method stub
		return null;
	}
}
